package com.kawa.kawa_ad.handler;

import android.app.Activity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.anythink.interstitial.api.ATInterstitialListener;
import com.anythink.splashad.api.ATSplashAd;
import com.anythink.splashad.api.ATSplashAdExtraInfo;
import com.anythink.splashad.api.ATSplashAdListener;
import com.kawa.kawa_ad.util.Utils;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;

public class IntersHandler implements ATInterstitialListener {
    private final Activity mActivity;
    private EventChannel.EventSink mEventSink;
    private final ATInterstitial mIntersAd;
    private final BinaryMessenger mMessenger;

    public IntersHandler(Activity activity, BinaryMessenger messenger, String intersId){
        mActivity = activity;
        mMessenger = messenger;
        mIntersAd = new ATInterstitial(activity,intersId);
        mIntersAd.setAdListener(this);
    }

    public void load(int channelId){
        setupEventSink(channelId);
        mIntersAd.load();
    }

    private void setupEventSink(int channelId){
        EventChannel mEventChannel = new EventChannel(mMessenger, "com.mob.adsdk/event_" + channelId);
        mEventChannel.setStreamHandler(new EventChannel.StreamHandler() {
            @Override
            public void onListen(Object o, EventChannel.EventSink eventSink) {
                mEventSink = eventSink;
            }

            @Override
            public void onCancel(Object o) {
                mEventSink = null;
            }
        });
    }

    public void show(int channelId,String scene){
        setupEventSink(channelId);
        if (mIntersAd.isAdReady()){
            mIntersAd.show(mActivity,scene);
        }else if (mIntersAd.checkAdStatus().isLoading()){
            Map<String,Object> map = new HashMap<>();
            map.put("name","showLoading");
            map.put("result","0");
            mEventSink.success(map);
            closeSink();
        }else{
            Map<String,Object> map = new HashMap<>();
            map.put("name","showFail");
            map.put("result","1");
            mEventSink.success(map);
            closeSink();
            mIntersAd.load();
        }
    }

    private void closeSink(){
        if (mEventSink != null) {
            mEventSink.endOfStream();
            mEventSink = null;
        }
    }

    @Override
    public void onInterstitialAdLoaded() {
        Map<String,Object> map = new HashMap<>();
        map.put("name","loaded");
        map.put("result","0");
        mEventSink.success(map);
        closeSink();
    }

    @Override
    public void onInterstitialAdLoadFail(AdError adError) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","fail");
        map.put("result","1");
        map.put("error", adError.getDesc());
        mEventSink.success(map);
        closeSink();
    }

    @Override
    public void onInterstitialAdClicked(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","click");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onInterstitialAdShow(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","show");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onInterstitialAdClose(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","close");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
        closeSink();
        mIntersAd.load();
    }

    @Override
    public void onInterstitialAdVideoStart(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","start");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onInterstitialAdVideoEnd(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","videoEnd");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
        closeSink();
    }

    @Override
    public void onInterstitialAdVideoError(AdError adError) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","videoError");
        map.put("result","0");
        map.put("error", adError.getDesc());
        mEventSink.success(map);
        closeSink();
    }
}
